package com.ccom.core.common.util;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 序列化工具
 **/
public class HessianSerializationUtils {
    public HessianSerializationUtils() {
    }

    /**
     * 序列化
     *
     * @param msg
     * @return
     * @throws IOException
     */
    public static byte[] serialize(Object msg) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        HessianOutput out = new HessianOutput(bout);
        out.writeObject(msg);
        return bout.toByteArray();
    }

    /**
     * 反序列化
     *
     * @param bytes
     * @return
     * @throws IOException
     */
    public static Object deserialize(byte[] bytes) throws IOException {
        if (bytes == null) {
            throw new NullPointerException();
        } else {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes);
            HessianInput hin = new HessianInput(input);
            return hin.readObject();
        }
    }
}